/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package network.packets;

import java.util.Vector;
import utils.Utils;
import datamodel.Statistic;
import utils.StringUtils;

/**
 *
 * @author Kvest
 */
public class TaxiParkingStatisticResponce extends Packet
{
    private Vector statistics;
    
    public TaxiParkingStatisticResponce(byte[] data)
    {
        super(TAXI_PARKING_STATISTIC_RESPONCE);
        statistics = new Vector();

        parse(data);
    }

    protected void parse(byte[] data)
    {
        int offset = 0;

        //Пропускаем название пакета
        byte[] buffer4 = new byte[4];
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);

        //Количество
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        int count =  Utils.byteToInt(buffer4);

        //id
        for (int j = 0; j < count; j++)
        {
            System.arraycopy(data, offset, buffer4, 0, buffer4.length);
            offset += buffer4.length;
            addStatistic(new Statistic(Utils.byteToInt(buffer4)));
        }

        //count
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        count =  Utils.byteToInt(buffer4);

        buffer4[2] = 0;
        buffer4[3] = 0;
        for (int j = 0; j < count; ++j)
        {
            buffer4[0] = data[offset++];
            buffer4[1] = data[offset++];
            getStatistic(j).setCount(Utils.byteToInt(buffer4));
        }

        //Список позывных на стоянке
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        count =  Utils.byteToInt(buffer4);
        
        byte[] buffer;
        for (int j = 0; j < count; j++)
        {
            System.arraycopy(data, offset, buffer4, 0, buffer4.length);
            offset += buffer4.length;
            buffer = new byte[Utils.byteToInt(buffer4)];
            
            System.arraycopy(data, offset, buffer, 0, buffer.length);
            offset += buffer.length;
            
            getStatistic(j).setQueue(StringUtils.bytesToStr(buffer));
        }
        
        buffer4 = null;
        buffer = null;
    }

    public void addStatistic(Statistic value)
    {
        statistics.addElement(value);
    }

    public Statistic getStatistic(int index)
    {
        return (Statistic)statistics.elementAt(index);
    }

    public Vector getStatistics()
    {
        return statistics;
    }
}
